package com.tanhua.api.mongo;

import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override  //查询mongdb数据库
    public RecommendUser findMaxScore(Long userId) {

        Query query = new Query(Criteria.where("toUserId").is(userId));
        query.with(new Sort(Sort.Direction.DESC,"score"));//排序 score 降序
        query.skip(0).limit(1);
        return mongoTemplate.findOne(query,RecommendUser.class);
    }

    @Override //查询多个佳人信息
    public PageBeanVo findScorePage(Long userId, Integer page, Integer pagesize) {

        Query query = new Query(Criteria.where("toUserId").is(userId));
        query.with(new Sort(Sort.Direction.DESC,"score"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);

        long count = mongoTemplate.count(query, RecommendUser.class);

        PageBeanVo pb = new PageBeanVo();
        pb.setItems(list); //RecommendUser
        pb.setPage(page);
        pb.setPagesize(pagesize);
        pb.setCounts(count);
        return pb;
    }

    @Override
    public Long findScore(Long id, Long jrid) {
        Query query = new Query(Criteria.where("toUserId").is(id)
        .and("userId").is(jrid));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if(recommendUser==null){
            return 99L;
        }
        return recommendUser.getScore().longValue();
    }
}
